import { DaoBase } from "../../src/engine/dao/DaoBase";
import DateUtil from "../../src/engine/utils/DateUtil";
import { Logger } from "../../src/engine/utils/Logger";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";

export class AccountDailyLogin extends DaoBase {

    public static Tb_Name: string = "t_accounts_dailylogin";

    public static async isExistRecord(date: string, account: string) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getLogDbName());
        let sql: string = "select * from " + this.Tb_Name + " where account=? and date=? ;"
        let sqlParams = [account, date];
        let results = await client.queryPromise(sql, sqlParams);
        Logger.log("isExistRecord", sql, sqlParams, results)
        if (results && results.length > 0) {
            return true
        }
        else {
            return false;
        }
    }

    public static async insertAccountDailyLogin(date: string, account: string) {
        let isExist: boolean = await this.isExistRecord(date, account)
        if (!isExist) {
            let client: MysqlClient = await MysqlManager.instance.getDb(this.getLogDbName());
            let sql: string = "insert into " + this.Tb_Name + "(date,account) VALUES(?,?) ;"
            let sqlParams = [date, account];
            let results = await client.queryPromise(sql, sqlParams);
            Logger.log("insertAccountDailyLogin", sql, sqlParams, results)
            if (results && results["affectedRows"] > 0) {
                return true
            } else {
                return false
            }
        }
    }

    public static async getAccountLogin(startDate:string, endDate:string){
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getLogDbName());
        let sql: string = "select * from " + this.Tb_Name + " where date >= ? and date <= ?;"
        let sqlParams = [startDate, endDate];
        let results = await client.queryPromise(sql, sqlParams);
        return results;
    }


    public static async clear(delete_time: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getLogDbName());
        let sql: string = "delete from "+this.Tb_Name+ " where date < ?"
        let sqlParams = [DateUtil.formatDateStr3(delete_time)];
        await  client.queryPromise(sql, sqlParams);
    }


}